# -*- coding: utf-8 -*-
# @Time    : 2022/4/13 11:29
# @Author  : zyw
# @Email   : panda_kings@163.com
# @File    : collect.py
from flask_restful import Api,Resource,reqparse
from flask import Blueprint,jsonify

# from common.models.news import NewsInfo,Collect
from common.models.users import User
from common.models import db

collect_bp = Blueprint('collect',__name__, url_prefix='/collect')

api = Api(collect_bp)


# class CollectResource(Resource):
#     def post(self):
#         """添加收藏"""
#         # 获取用户收藏的文章id
#         parse = reqparse.RequestParser()
#         parse.add_argument('nid',type=int, location='form')
#         parse.add_argument('uid', type=int, location='form')
#         args = parse.parse_args()
#         new_id = args.get('nid')
#         user_id =args.get('uid')
#         news = NewsInfo.query.filter_by(nid=new_id).first()
#         if not news:
#             return jsonify({'msg':'文章不存在'})
#         user = User.query.filter_by(uid=user_id).first()
#         if not user:
#             return jsonify({'msg':"用户不存在"})
#         collect = Collect(user_id=user.id,article_id=news.id)
#         db.session.add(collect)
#         db.session.commit()
#
#     def get(self):
#         """获取用户所有收藏"""
#         parse = reqparse.RequestParser()
#         parse.add_argument('uid', type=int, location='form')
#         args = parse.parse_args()
#         user_id = args.get('uid')
#         # 根据用户id获取收藏
#         user = User.query.filter_by(uid=user_id).first()
#         if not user:
#             return jsonify({'msg': "用户不存在"})
#         collects = user.collect.all()
#         collect_arts = []
#         for collect in collects:
#             # 判断文章是否被删除了
#             news = NewsInfo.query.filter_by(nid=collect.article_id).first()
#             if news:
#                 collect_arts.append({
#                     'nid':news.nid,
#                     'title': news.title,
#                     'comment_count': news.comment_count
#                 })
#         """
#         {
#             "mgs":'xx',
#             "code':200,
#             'data';[
#                 {
#                     'nid':1,
#                     'title':"numpy'
#                 }
#             ]
#         }
#         """
#         return jsonify({'msg':"获取收藏文章","code":200, "data":collect_arts})
#
#
#     def delete(self):
#         """取消收藏"""
#         parse = reqparse.RequestParser()
#         parse.add_argument('uid', type=int, location='args')
#         parse.add_argument('nid', type=int, location='args')
#         args = parse.parse_args()
#         user_id = args.get('uid')
#         news_id = args.get('nid')
#         # 根据用户id获取收藏
#         user = User.query.filter_by(uid=user_id).first()
#         if not user:
#             return jsonify({'msg': "用户不存在"})
#
#         # 更改搜长的状态
#         Collect.query.filter_by(user_id=user.id, article_id=news_id).update({'is_delete':True})
#         db.session.commit()



# 添加收藏
# api.add_resource(CollectResource,'/collect')





